package com.by.controller;

import com.by.bean.Dict;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.V;
import com.by.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = "字典管理")
public class DictController extends BaseController{
    @Autowired
    DictService dictService;
    @ApiOperation("保存字典")
    @PostMapping("/dict")
    public ResultBean save(@Validated @RequestBody Dict dict , BindingResult result){
        V.validate(result);
        int rs=dictService.save(dict);
        return saveResult(rs);
    }
    @ApiOperation("根据父id查询字典")
    @GetMapping("/dict/child")
    public ResultBean<List<Dict>> query(@RequestParam(required = true) int pid){
        List list = dictService.query(pid);
        return success("字典列表",list);
    }
    @ApiOperation("修改字典")
    @PutMapping("/dict")
    public ResultBean update(@Validated @RequestBody Dict dict , BindingResult result){
        V.validate(result);
        int rs=dictService.update(dict);
        return saveResult(rs);
    }
    @ApiOperation("删除字典")
    @DeleteMapping("/dict")
    public ResultBean delete(@RequestParam(required = true) Serializable id){
        int rs=dictService.delete(id);
        return deleteResult(rs);
    }
}
